package boun.swe.bob.dao;

import boun.swe.bob.entities.User;
import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * This DAO corresponds to users table in database
 */
@Component
public class UserDao extends GenericDaoImpl<User, Integer> {

    private static final Logger LOG = Logger.getLogger(User.class);

    /**
     * This method returns the users record by specifying a username
     *
     * @param username user name
     * @return the record
     */
    public User findByUsername(final String username) {
        return (User) hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery("select * from users u where u.username = ?").addEntity("u", User.class);
                query.setString(0, username);
                return query.uniqueResult();
            }
        });
    }

    /**
     * This method returns the users record by specifying the location
     *
     * @param location location
     * @return the record
     */
    public List<User> findUsersByLocation(final String location) {
        return (List<User>) hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery("select * from users u where u.location = ?").addEntity("u", User.class);
                query.setString(0, location);
                return query.list();
            }
        });
    }

    /**
     * This method encrypts the password with MD5 encryption
     *
     * @param password password
     * @return encrypted text
     */
    public String createMd5Password(final String password) {
        return (String) hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery("select md5('" + password + "') md5 from dual").addScalar("md5", Hibernate.STRING);
                return query.uniqueResult();
            }
        });
    }

    public List<User> findUsersForSearch(final String keyword) {
        return (List<User>) hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Set<User> searchset = new HashSet<User>();
                
                Query query = session.createSQLQuery("select * from users u where u.name like '%" + keyword + "%' ").addEntity("u", User.class);
                List<User> namelist = query.list();
                searchset.addAll(namelist);

                query = session.createSQLQuery("select * from users u where u.username like '%" + keyword + "%' ").addEntity("u", User.class);
                List<User> usernameList = query.list();
                searchset.addAll(usernameList);

                query = session.createSQLQuery("select * from users u where u.surname like '%" + keyword + "%' ").addEntity("u", User.class);
                List<User> surnameList = query.list();
                searchset.addAll(surnameList);

                query = session.createSQLQuery("select * from users u where u.location like '%" + keyword + "%' ").addEntity("u", User.class);
                List<User> locationList = query.list();
                searchset.addAll(locationList);

                return new ArrayList<User>(searchset);
            }
        });
    }


    public List<User> findUnapprovedUsers() {
        return (List<User>) hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery("select * from users u where u.enabled = '0'").addEntity("u", User.class);
                return query.list();
            }
        });
    }


    
}
